import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:fantuike_app/api/Api.dart' show Api;
import 'package:flutter/cupertino.dart';
import 'package:fantuike_app/constants.dart' show Constants;

class ActivityPage extends StatefulWidget {
  @override
  _ActivityPageState createState() => _ActivityPageState();
}

class _ActivityPageState extends State<ActivityPage>
    with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin {
  TabController _tabController;
  ScrollController _scrollController;

  List activityList = [];

  List closeActivityList = [];

  int resultCode;

  @override
  bool get wantKeepAlive => true;

  void initState() {
    super.initState();
    _tabController = new TabController(vsync: this, length: 2);
    _scrollController = new ScrollController(initialScrollOffset: 0.0);
    _getActivityList();
    print(resultCode);
  }

  @override
  void dispose() {
    super.dispose();
    _tabController.dispose();
    _scrollController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        child: Container(
          color: Color(0xfff26966),
          alignment: Alignment.center,
          child: Text(
            "活动列表",
            style: TextStyle(color: Color(0xffFCFCFE), fontSize: 18.0),
          ),
        ),
      ),
      body: NestedScrollView(
        controller: _scrollController,
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return <Widget>[
            SliverAppBar(
              pinned: true,
              floating: true,
              expandedHeight: 40.0,
              flexibleSpace: FlexibleSpaceBar(
                collapseMode: CollapseMode.pin,
                background: Container(
                  height: double.infinity,
                  color: Color(0xffcccccc),
                  child: Column(
                    children: <Widget>[],
                  ),
                ),
              ),
              bottom: TabBar(
                controller: _tabController,
                indicatorSize: TabBarIndicatorSize.label,
                labelColor: Color(0xfff26966),
                indicatorColor: Color(0xfff26966),
                unselectedLabelColor: Color(0xff000000),
                tabs: [
                  Tab(
                    text: "正在进行",
                  ),
                  Tab(text: "已结束"),
                ],
                indicatorPadding: const EdgeInsets.only(bottom: 5.0),
              ),
            )
          ];
        },
        body: Container(
          margin: const EdgeInsets.all(10.0),
          child: TabBarView(
            controller: _tabController,
            children: <Widget>[
              _buildListView("comming", activityList),
              _buildListView("end", activityList),
            ],
          ),
        ),
      ),
    );
  }

  void _getActivityList() {
    getActivityList().then((data) {
      setState(() {
        activityList.addAll(data['extend']['message']);
        print(data['extend']['message']);
      });
    });
  }

  Future getActivityList() async {
    try {
      Response response = await Dio().get(Api.ActivityList);
      return response.data;
    } catch (e) {
      print(e);
    }
  }
}

// 构建自定义AppBar
class MyAppBar extends StatefulWidget implements PreferredSizeWidget {
  final Widget child;

  MyAppBar({@required this.child}) : assert(child != null);

  @override
  _MyAppBarState createState() => _MyAppBarState();

  @override
  Size get preferredSize => new Size.fromHeight(45.0);
}

class _MyAppBarState extends State<MyAppBar> {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: widget.child,
      top: true,
    );
  }
}

Widget _buildListView(String tip, List activityList) {
  if (tip == "comming") {
    if (activityList.length != 0) {
      return ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            return GestureDetector(
              onTap: () {
                print("跳转到详情页面");
              },
              child: Container(
                alignment: Alignment.center,
                height: 200.0,
                margin: const EdgeInsets.only(bottom: 10.0),
                // color: Colors.redAccent,
                child: Card(
                  elevation: 2.0,
                  child: Column(
                    children: <Widget>[
                      Container(
                        margin: const EdgeInsets.only(
                            top: 10.0, left: 10.0, right: 10.0, bottom: 10.0),
                        child: Image.network(
                          activityList[index]['avatar'],
                          fit: BoxFit.fill,
                        ),
                      ),
                      Container(
                        margin: const EdgeInsets.only(left: 10.0, right: 10.0),
                        alignment: Alignment.centerLeft,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Text(
                              activityList[index]['name'],
                              style: TextStyle(
                                  color: Color(0xff252525), fontSize: 16.0),
                            ),
                            Text(activityList[index]['closeDate'],
                                style: TextStyle(
                                    color: Color(0xffAAAAAA), fontSize: 14.0))
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            );
          },
          itemCount: activityList.length);
    } else {
      return CupertinoActivityIndicator();
    }
  } else {
    if (activityList.length != 0) {
      return ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            return GestureDetector(
              onTap: () {
                print("跳转到详情页面");
              },
              child: Container(
                alignment: Alignment.center,
                height: 200.0,
                margin: const EdgeInsets.only(bottom: 10.0),
                // color: Colors.redAccent,
                child: Card(
                  elevation: 2.0,
                  child: Column(
                    children: <Widget>[
                      Container(
                        margin: const EdgeInsets.only(
                            top: 10.0, left: 10.0, right: 10.0, bottom: 10.0),
                        child: Image.network(
                          activityList[index]['avatar'],
                          fit: BoxFit.fill,
                        ),
                      ),
                      Container(
                        margin: const EdgeInsets.only(left: 10.0, right: 10.0),
                        alignment: Alignment.centerLeft,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Text(
                              activityList[index]['name'],
                              style: TextStyle(
                                  color: Color(0xff252525), fontSize: 16.0),
                            ),
                            Text(activityList[index]['closeDate'],
                                style: TextStyle(
                                    color: Color(0xffAAAAAA), fontSize: 14.0))
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            );
          },
          itemCount: activityList.length);
    }
  }
}
